package com.example.demo4

import android.content.Intent
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity

class PersonInformationActivity : AppCompatActivity() {
    
    private lateinit var etName: EditText
    private lateinit var etStudentId: EditText
    private lateinit var etPhone: EditText
    private lateinit var etEmail: EditText
    private lateinit var rgGender: RadioGroup
    private lateinit var rbMale: RadioButton
    private lateinit var rbFemale: RadioButton
    private lateinit var cbTravel: CheckBox
    private lateinit var cbReading: CheckBox
    private lateinit var cbGaming: CheckBox
    private lateinit var cbShopping: CheckBox
    private lateinit var btnSave: Button
    private lateinit var btnCancel: Button
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_person_information)
        
        initViews()
        loadData()
        setupListeners()
    }
    
    private fun initViews() {
        etName = findViewById(R.id.etName)
        etStudentId = findViewById(R.id.etStudentId)
        etPhone = findViewById(R.id.etPhone)
        etEmail = findViewById(R.id.etEmail)
        rgGender = findViewById(R.id.rgGender)
        rbMale = findViewById(R.id.rbMale)
        rbFemale = findViewById(R.id.rbFemale)
        cbTravel = findViewById(R.id.cbTravel)
        cbReading = findViewById(R.id.cbReading)
        cbGaming = findViewById(R.id.cbGaming)
        cbShopping = findViewById(R.id.cbShopping)
        btnSave = findViewById(R.id.btnSave)
        btnCancel = findViewById(R.id.btnCancel)
    }
    
    private fun loadData() {
        val name = intent.getStringExtra(MainActivity.EXTRA_NAME)
        val gender = intent.getStringExtra(MainActivity.EXTRA_GENDER)
        val studentId = intent.getStringExtra(MainActivity.EXTRA_STUDENT_ID)
        val phone = intent.getStringExtra(MainActivity.EXTRA_PHONE)
        val email = intent.getStringExtra(MainActivity.EXTRA_EMAIL)
        val hobbies = intent.getStringExtra(MainActivity.EXTRA_HOBBIES)
        
        // 设置姓名、学号、电话、邮箱
        if (name != getString(R.string.default_name)) {
            etName.setText(name)
        }
        if (studentId != getString(R.string.default_student_id)) {
            etStudentId.setText(studentId)
        }
        if (phone != getString(R.string.default_phone)) {
            etPhone.setText(phone)
        }
        if (email != getString(R.string.default_email)) {
            etEmail.setText(email)
        }
        
        // 设置性别
        when (gender) {
            getString(R.string.radio_male) -> rbMale.isChecked = true
            getString(R.string.radio_female) -> rbFemale.isChecked = true
        }
        
        // 设置爱好
        if (hobbies != null && hobbies != getString(R.string.default_hobbies)) {
            cbTravel.isChecked = hobbies.contains(getString(R.string.hobby_travel))
            cbReading.isChecked = hobbies.contains(getString(R.string.hobby_reading))
            cbGaming.isChecked = hobbies.contains(getString(R.string.hobby_gaming))
            cbShopping.isChecked = hobbies.contains(getString(R.string.hobby_shopping))
        }
    }
    
    private fun setupListeners() {
        btnSave.setOnClickListener {
            saveData()
        }
        
        btnCancel.setOnClickListener {
            setResult(RESULT_CANCELED)
            finish()
        }
    }
    
    private fun saveData() {
        val name = etName.text.toString().ifEmpty { getString(R.string.default_name) }
        val studentId = etStudentId.text.toString().ifEmpty { getString(R.string.default_student_id) }
        val phone = etPhone.text.toString().ifEmpty { getString(R.string.default_phone) }
        val email = etEmail.text.toString().ifEmpty { getString(R.string.default_email) }
        
        val gender = if (rbMale.isChecked) {
            getString(R.string.radio_male)
        } else {
            getString(R.string.radio_female)
        }
        
        val hobbies = buildString {
            val selectedHobbies = mutableListOf<String>()
            if (cbTravel.isChecked) selectedHobbies.add(getString(R.string.hobby_travel))
            if (cbReading.isChecked) selectedHobbies.add(getString(R.string.hobby_reading))
            if (cbGaming.isChecked) selectedHobbies.add(getString(R.string.hobby_gaming))
            if (cbShopping.isChecked) selectedHobbies.add(getString(R.string.hobby_shopping))
            
            if (selectedHobbies.isEmpty()) {
                append(getString(R.string.default_hobbies))
            } else {
                append(selectedHobbies.joinToString(separator = ", "))
            }
        }
        
        val resultIntent = Intent().apply {
            putExtra(MainActivity.EXTRA_NAME, name)
            putExtra(MainActivity.EXTRA_GENDER, gender)
            putExtra(MainActivity.EXTRA_STUDENT_ID, studentId)
            putExtra(MainActivity.EXTRA_PHONE, phone)
            putExtra(MainActivity.EXTRA_EMAIL, email)
            putExtra(MainActivity.EXTRA_HOBBIES, hobbies)
        }
        
        setResult(RESULT_OK, resultIntent)
        finish()
    }
}

